package com.wanglei.educourseboot.service.impl;

import com.wanglei.educourseboot.entity.Course;
import com.wanglei.educourseboot.mapper.CourseMapper;
import com.wanglei.educourseboot.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author:wanglei
 * @create:2022-05-24-16:51
 * @Description:
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public List<Course> getAllCourse() {
        System.out.println("redis查询");
        List<Course> list = (List<Course>) redisTemplate.opsForValue().get("allCourse");
        if (list == null){
            synchronized (this){
                list = (List<Course>) redisTemplate.opsForValue().get("allCourse");
                if (list == null){
                    System.out.println("MySQL查询");
                    list = courseMapper.getAllCourse();
                    redisTemplate.opsForValue().set("allCourses", list,10, TimeUnit.SECONDS);
                }
            }
        }
        return list;
    }

    @Override
    public List<Course> getCourseByUserId(String userId) {
        List<Course> courseByUserId = courseMapper.getCourseByUserId(userId);
        return courseByUserId;
    }

    @Override
    public Course getCourseById(Integer courseid) {
        Course courseById = courseMapper.getCourseById(courseid);
        return courseById;
    }
}
